var dragging = function (settings){
	typeof(settings) == 'undefined' ? settings = {} : '';
	this.move = 'both'; //可选值为X、Y 、both
	this.hander = 1; // 1 全部可拖拽
	this.changeSize = false; // 是否可改变大小
	this.restricted = true; // 是否限制区域
	this.reIndex = true;
	this.oriPosition = {}
	this.supportDelete = false;
	this.border = '1px solid #fff'
	$.extend( this,settings );
}
dragging.prototype = {
	init : function(jQueryObj,callback){
		var self = this;
		self.father = jQueryObj.parent();
		self.dragEl = jQueryObj;
		self.img = self.dragEl.find('img');
		var timer = null;
		
		timer = setInterval(function(){
			var loaded = true;
			if(self.img.height() == 0){
				loaded = false;
			}
			if(loaded){
				clearInterval(timer);
				self.initFun(jQueryObj,callback);
			}
		},100)
		
	}
	,initFun : function(jQueryObj,callback){
		var self = this;
		self.data = {
			X : '', //移动之前的pageX
			Y : '', //移动之前的pageY
			xPage : '', //移动之后的pageX
			yPage : '', //移动之后的pageY
			positionX : '',//移动之前的positionX
			positionY : '',//移动之前的positionY
			moveX : '',//移动之后的positionX
			moveY : '',//移动之后的positionY
			disX : '',//X变化量
			dixY : '',//Y变化量
			faWidth : self.father.width(),
			faHeight : self.father.height(),
			elWidth : self.img.outerWidth()+2,
			elHeight : self.img.outerHeight()+2,
			rate : self.img.outerWidth()/self.img.outerHeight()
		}
		
		self.resize = false; // 是否改变大小
		self.bBtn = true; // resize开关
		self.hasCal = false; // 是否重新计算拖拽 or 改变大小
		self.hander = self.hander == 1 ?  jQueryObj : jQueryObj.find(self.hander);

		// 初始化样式
		self.father.css({"position":"relative","overflow":"hidden"});
		self.dragEl.css({
			"position":"absolute",
			"width" : self.data.elWidth,
			"height" : self.data.elHeight
		});
		if(!$.isEmptyObject(self.oriPosition)){
			self.dragEl.css({
				"left" : self.oriPosition.left,
				"top" : self.oriPosition.top
			});
		}
		self.img.css({
			'display' : 'block',
			'width' : "100%"
		})

		self.father.children().css({
			'border' : '1px solid transparent',
			'margin' : '-1px'
		});
		// 初始化鼠标形状
		$(document).mousemove(function(e){
			if(self.bBtn){
				self.moveResizeInit(e);
			}
		})
		
		// 鼠标按下
		self.hander.mousedown(function(e){
			self.mouseDownFn(e);
			//鼠标拖动 
			$(document).mousemove(function(e){
				self.mouseMoveFn(e);
			})
			// 鼠标抬起
			$(document).mouseup(function(e){
				self.bBtn = true;
				self.hasCal = false;
				document.onmousemove = null;
				document.onmouseup = null;
			})
			return false;
		});

		if(self.supportDelete){
			self.deleteEl();
		}
		if(callback){
			callback();
		}
	}
	,mouseDownFn : function(e){
		console.log(2)
		var self = this;
		self.data.X = e.pageX;
		self.data.Y = e.pageY;
		self.data.positionX = self.dragEl.position().left;
		self.data.positionY = self.dragEl.position().top;
		self.data.elWidth = self.dragEl.width();
		self.data.elHeight = self.dragEl.height();
		self.bBtn = false;
		if(self.reIndex){
			self.father.children().css({
				'zIndex' : '0'
			});
			self.dragEl.css({
				'zIndex' : '1'
			});
		}
		self.father.children().attr('data-choosen','false');
		self.father.children().css({
			'border' : '1px solid transparent'
		});
		
		self.dragEl.css({
			'border' : self.border
		});
		if(self.supportDelete){
			self.dragEl.attr('data-choosen','true');
		}
	}
	,mouseMoveFn : function(e){
		var self = this;
		self.data.sDisX = e.pageX - self.data.xPage;
		self.data.sDisY = e.pageY - self.data.yPage;
		self.data.xPage = e.pageX;
		self.data.yPage = e.pageY;
		self.data.disX = self.data.xPage - self.data.X;
		self.data.disY = self.data.yPage - self.data.Y;
		self.data.moveX = self.data.positionX + self.data.disX;
		self.data.moveY = self.data.positionY + self.data.disY;

		if(!self.bBtn && !self.hasCal){ // 判断是拖拽还是改变大小
			var R = self.dragEl.position().left + self.dragEl.width() - 10;
			var B = self.dragEl.position().top + self.dragEl.height() - 10;
			var T = self.dragEl.position().top + 10;
			var L = self.dragEl.position().left + 10; 
			
			if(e.pageX - self.father.offset().left > R && e.pageY - self.father.offset().top > B){ // 右下角
				self.dragEl.css({"cursor":"se-resize"});
				self.resize = 'se';
			}
			else if (e.pageX - self.father.offset().left > R && e.pageY - self.father.offset().top < T){ // 右上角
				self.dragEl.css({"cursor":"ne-resize"});
				self.resize = 'ne';
			}
			else if (e.pageX - self.father.offset().left < L && e.pageY - self.father.offset().top < T){ // 左上角
				self.dragEl.css({"cursor":"nw-resize"});
				self.resize = 'nw';
			}
			else if (e.pageX - self.father.offset().left < L && e.pageY - self.father.offset().top > B){ // 左下角
				self.dragEl.css({"cursor":"sw-resize"});
				self.resize = 'sw';
			}
			else{
				self.resize = false;
			}

			self.hasCal = true;
		}
		
		
		if(!!self.resize  && !self.bBtn){ // 改变大小
			// 等比例拖拽
			self.thisResize(self.resize,e);
		}else if(!self.resize && !self.bBtn){ //其余位置
			if(self.move.toLowerCase() == "x"){
				self.thisXMove();
			}else if(self.move.toLowerCase() == "y"){
				self.thisYMove();
			}else if(self.move.toLowerCase() == 'both'){
				self.thisAllMove();
			}
		}
		
	}
	,thisResize : function(dir,e){
		var self = this;
		var direction = 'x';
		
		if(dir == 'se'){
			var newX = self.data.xPage + self.data.elWidth - self.data.X;
			var newY = self.data.yPage + self.data.elHeight - self.data.Y;
			if(newY != 0 && newX/newY < self.data.rate){
				direction = 'y';
			}
			if(direction == 'x'){
				self.dragEl.css({
					"width" : self.data.elWidth + self.data.disX,
					"height" : self.data.elHeight + Math.ceil(self.data.disX * 1/self.data.rate)
				});
			}else{
				self.dragEl.css({
					"width" : self.data.elWidth + Math.ceil(self.data.disY * self.data.rate),
					"height" : self.data.elHeight + self.data.disY
				});
			}
		}
		else if(dir == 'ne'){
			var newX = self.data.xPage - self.data.X + self.data.elWidth;
			var newY = self.data.elHeight + self.data.Y - self.data.yPage ;

			if(newY != 0 && newX/newY < self.data.rate){
				direction = 'y';
			}
			if(direction == 'x'){
				self.dragEl.css({
					"width" : self.data.elWidth + self.data.disX,
					"height" : self.data.elHeight + Math.ceil(self.data.disX * 1/self.data.rate),
					"top" : self.data.positionY - Math.ceil(self.data.disX * 1/self.data.rate)
				});
				
			}else{
				self.dragEl.css({
					"width" : self.data.elWidth - Math.ceil(self.data.disY * self.data.rate),
					"height" : self.data.elHeight - self.data.disY,
					"top" : self.data.positionY + self.data.disY
				});
			}
		}
		else if(dir == 'nw'){
			var newX = self.data.X - self.data.xPage + self.data.elWidth;
			var newY = self.data.elHeight + self.data.Y - self.data.yPage ;

			if(newY != 0 && newX/newY < self.data.rate){
				direction = 'y';
			}
			if(direction == 'x'){
				self.dragEl.css({
					"width" : self.data.elWidth - self.data.disX,
					"height" : self.data.elHeight - Math.ceil(self.data.disX * 1/self.data.rate),
					"top" : self.data.positionY + Math.ceil(self.data.disX * 1/self.data.rate),
					"left" : self.data.positionX + self.data.disX
				});
			}else{
				self.dragEl.css({
					"width" : self.data.elWidth - Math.ceil(self.data.disY * self.data.rate),
					"height" : self.data.elHeight - self.data.disY,
					"top" : self.data.positionY + self.data.disY,
					"left" : self.data.positionX + Math.ceil(self.data.disY * self.data.rate)
				});
			}

		}
		else if(dir == 'sw'){
			var newX = self.data.X - self.data.xPage + self.data.elWidth;
			var newY = self.data.yPage - self.data.Y + self.data.elHeight;

			if(newY != 0 && newX/newY < self.data.rate){
				direction = 'y';
			}
			if(direction == 'x'){
				self.dragEl.css({
					"width" : self.data.elWidth - self.data.disX,
					"height" : self.data.elHeight - Math.ceil(self.data.disX * 1/self.data.rate),
					"left" : self.data.positionX + self.data.disX

				});
			}else{
				self.dragEl.css({
					"width" : self.data.elWidth + Math.ceil(self.data.disY * self.data.rate),
					"height" : self.data.elHeight + self.data.disY,
					"left" : self.data.positionX - Math.ceil(self.data.disY * self.data.rate)
				});
			}
		}
	}
	,moveResizeInit : function(e){
		
		var self = this;
		if(self.changeSize){
			var R = self.dragEl.position().left + self.dragEl.width() - 10;
			var B = self.dragEl.position().top + self.dragEl.height() - 10;
			var T = self.dragEl.position().top + 10;
			var L = self.dragEl.position().left + 10;

			if(e.pageX - self.father.offset().left > R && e.pageY - self.father.offset().top > B){ // 右下角
				self.dragEl.css({"cursor":"se-resize"});
			}
			else if (e.pageX - self.father.offset().left > R && e.pageY - self.father.offset().top < T){ // 右上角
				self.dragEl.css({"cursor":"ne-resize"});
			}
			else if (e.pageX - self.father.offset().left < L && e.pageY - self.father.offset().top < T){ // 左上角
				self.dragEl.css({"cursor":"nw-resize"});
			}
			else if (e.pageX - self.father.offset().left < L && e.pageY - self.father.offset().top > B){ // 左下角
				self.dragEl.css({"cursor":"sw-resize"});
			}
			else{
				self.hander.css({"cursor":"move"});
			}
		}else{
			self.hander.css({"cursor":"move"});
		}	
		
		
		
	}
	,thisXMove : function(){ //x轴移动
		var self = this;
		
		if(!self.bBtn){
			self.dragEl.css({"left":self.data.moveX});
		}else{
			return;
		}
		if(self.restricted){
			if(self.data.moveX < 0){
				self.dragEl.css({"left":"0"});
			}
			if(self.data.moveX > (self.data.faWidth-self.data.elWidth)){
				self.dragEl.css({"left":self.data.faWidth-self.data.elWidth});
			}
		}
	}
	,thisYMove : function(){
		var self = this;
		if(!self.bBtn){
			self.dragEl.css({"top":self.data.moveY});
		}else{
			return;
		}
		if(self.restricted){
			if(self.data.moveY < 0){
				self.dragEl.css({"top":"0"});
			}
			if(self.data.moveY > (self.data.faHeight-self.data.elHeight)){
				self.dragEl.css({"top":self.data.faHeight-self.data.elHeight});
			}
		}
		
	}
	,thisAllMove : function(){
		var self = this;

		if(!self.bBtn){
			self.dragEl.css({
				"left":self.data.moveX+"px",
				"top":self.data.moveY+"px"
			});
		}else{
			return;
		}
		if(self.restricted){
			if(self.data.moveX < 0){
				self.dragEl.css({"left":"0"});
			}
			
			if(self.data.moveX > (self.data.faWidth-self.data.elWidth)){
				
				self.dragEl.css({"left":self.data.faWidth-self.data.elWidth});
			}

			if(self.data.moveY < 0){
				self.dragEl.css({"top":"0"});
			}
			if(self.data.moveY > (self.data.faHeight-self.data.elHeight)){
				self.dragEl.css({"top":self.data.faHeight-self.data.elHeight});
			}
		}
	}
	,deleteEl : function(obj){
		var self = this;
		document.onkeydown=function(e){
            if (e.keyCode == 46 || e.keyCode == 8){
               self.father.find("[data-choosen=true]").remove();
            }
        }
	}
};

$.fn.dragging = function(_settings_,_callback_){
	var _this = this;
	var options = $.extend( {}, $.fn.dragging.defaults, _settings_ );
	
	var	lmObj = new dragging(options);
		return 	lmObj.init( _this, _callback_ );
}